package com.sunrise.gateway.web.core;

import com.dap.utils.DateUtil;
import org.apache.commons.fileupload.FileItem;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

@Service
public class FileOSSEngine extends OSSEngineBase {
    public FileOSSEngine() {
        super();
    }

    @Override
    protected String handleFileItem(HttpServletRequest request, FileItem item) throws Exception {
        InputStream input = item.getInputStream();
        long length = item.getSize();

        String name;
        if (item.getName() != null) {
            name = item.getName().length() <= 32 ? item.getName() : item.getName().substring(0, 32);
        } else {
            name = "empty";
        }
        String fileName = "normalFile/" + DateUtil.format(new Date(), "yyyyMM") + "/" + UUID.randomUUID() + "/" + name;
        uploadService.putObject(ossConfig.getBucket(), fileName, input, length);
        input.close();

        return ossConfig.resolveFullUrl(fileName);
    }

}
